using System;
using System.Collections.Generic;
using System.Text;
using ERP.BusinessEntities.Base;
using ERP.DAL.Factory;
using System.Data;
namespace ERP.BusinessEntities
{

	public class BSNumberConfigsController:BaseBusinessController
	{
		public BSNumberConfigsController()
		{
			dal = FactoryDal.CreateEndDal("BSNumberConfigs");
		}

        public BSNumberConfigsInfo GetObject(String strModuleNo)
        {
            String strQuery = String.Format(@"SELECT * FROM BSNumberConfigs
                                                WHERE ModuleNo = '{0}'", strModuleNo);

            DataSet ds = GetDataset(strQuery);

            if(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                return (BSNumberConfigsInfo)GetObjectFromDataRow(ds.Tables[0].Rows[0]);

            return null;
        }


        public int GetMaxSuffix(string strTableName, String strPrefix)
        {
            int iMaxSuffix = 0;
            int iPrefixLen = strPrefix.Length;

            String strQuery = String.Format(@"
                                            SELECT MAX( SUBSTRING(No, {0}, LEN(No) - {1}))
                                            FROM {2}
                                            WHERE No like '{3}%'", iPrefixLen + 1, iPrefixLen - 1, strTableName, strPrefix);
            try
            {
                DataSet ds = GetDataset(strQuery);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    return Convert.ToInt32(ds.Tables[0].Rows[0][0]);

            }
            catch (Exception ex)
            {
            }

            return iMaxSuffix;
        }
	}
}
